{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt \n",
    "import pandas as pd\n",
    "from mpl_toolkits.mplot3d import Axes3D\n",
    "from scipy.interpolate import griddata\n",
    "from matplotlib import cm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 让图片中可以显示中文\n",
    "plt.rcParams['font.sans-serif'] = 'SimHei'\n",
    "# 让图片中可以显示负号\n",
    "plt.rcParams['axes.unicode_minus'] = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P2</th>\n",
       "      <th>P4</th>\n",
       "      <th>P7</th>\n",
       "      <th>P8</th>\n",
       "      <th>P9</th>\n",
       "      <th>P10</th>\n",
       "      <th>P11</th>\n",
       "      <th>P12</th>\n",
       "      <th>P13</th>\n",
       "      <th>P14</th>\n",
       "      <th>...</th>\n",
       "      <th>P29</th>\n",
       "      <th>P30</th>\n",
       "      <th>P31</th>\n",
       "      <th>P32</th>\n",
       "      <th>P33</th>\n",
       "      <th>P34</th>\n",
       "      <th>P35</th>\n",
       "      <th>P36</th>\n",
       "      <th>P37</th>\n",
       "      <th>P38</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.5</td>\n",
       "      <td>0.5144</td>\n",
       "      <td>7516.780273</td>\n",
       "      <td>41024.984375</td>\n",
       "      <td>13103.160156</td>\n",
       "      <td>104737.664062</td>\n",
       "      <td>12337.788086</td>\n",
       "      <td>98983.453125</td>\n",
       "      <td>11112.787109</td>\n",
       "      <td>89526.882812</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>374.969391</td>\n",
       "      <td>9960.210938</td>\n",
       "      <td>103.178192</td>\n",
       "      <td>5157.202148</td>\n",
       "      <td>199.192825</td>\n",
       "      <td>6606.042969</td>\n",
       "      <td>1900.335693</td>\n",
       "      <td>18949.912109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0290</td>\n",
       "      <td>7186.275391</td>\n",
       "      <td>36246.761719</td>\n",
       "      <td>11951.074219</td>\n",
       "      <td>89518.226562</td>\n",
       "      <td>11195.372070</td>\n",
       "      <td>84728.875000</td>\n",
       "      <td>10027.208008</td>\n",
       "      <td>76764.468750</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>125.636726</td>\n",
       "      <td>6339.634766</td>\n",
       "      <td>27.254568</td>\n",
       "      <td>4400.720703</td>\n",
       "      <td>60.425423</td>\n",
       "      <td>5197.659180</td>\n",
       "      <td>311.825806</td>\n",
       "      <td>13050.573242</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.5430</td>\n",
       "      <td>8334.772461</td>\n",
       "      <td>45327.628906</td>\n",
       "      <td>10205.735352</td>\n",
       "      <td>88953.109375</td>\n",
       "      <td>9503.373047</td>\n",
       "      <td>84162.937500</td>\n",
       "      <td>8453.971680</td>\n",
       "      <td>76219.468750</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>44.110538</td>\n",
       "      <td>7327.410156</td>\n",
       "      <td>3.616076</td>\n",
       "      <td>1053.424927</td>\n",
       "      <td>9.274529</td>\n",
       "      <td>1630.518555</td>\n",
       "      <td>11.553834</td>\n",
       "      <td>3542.782715</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.0</td>\n",
       "      <td>0.5144</td>\n",
       "      <td>14097.777344</td>\n",
       "      <td>76358.593750</td>\n",
       "      <td>27233.179688</td>\n",
       "      <td>272054.093750</td>\n",
       "      <td>25667.646484</td>\n",
       "      <td>258111.687500</td>\n",
       "      <td>23144.935547</td>\n",
       "      <td>234477.468750</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1253.976929</td>\n",
       "      <td>20884.535156</td>\n",
       "      <td>1221.493164</td>\n",
       "      <td>25805.855469</td>\n",
       "      <td>1620.159546</td>\n",
       "      <td>30538.640625</td>\n",
       "      <td>5010.981934</td>\n",
       "      <td>55917.894531</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0290</td>\n",
       "      <td>13418.339844</td>\n",
       "      <td>80313.000000</td>\n",
       "      <td>26595.847656</td>\n",
       "      <td>258812.203125</td>\n",
       "      <td>25071.730469</td>\n",
       "      <td>244930.578125</td>\n",
       "      <td>22614.595703</td>\n",
       "      <td>221877.000000</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1403.122559</td>\n",
       "      <td>24976.859375</td>\n",
       "      <td>904.027893</td>\n",
       "      <td>20220.417969</td>\n",
       "      <td>1234.554077</td>\n",
       "      <td>24038.703125</td>\n",
       "      <td>5224.838867</td>\n",
       "      <td>48107.187500</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 34 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    P2      P4            P7            P8            P9            P10  \\\n",
       "0  1.5  0.5144   7516.780273  41024.984375  13103.160156  104737.664062   \n",
       "1  1.5  1.0290   7186.275391  36246.761719  11951.074219   89518.226562   \n",
       "2  1.5  1.5430   8334.772461  45327.628906  10205.735352   88953.109375   \n",
       "3  2.0  0.5144  14097.777344  76358.593750  27233.179688  272054.093750   \n",
       "4  2.0  1.0290  13418.339844  80313.000000  26595.847656  258812.203125   \n",
       "\n",
       "            P11            P12           P13            P14  ...  P29  P30  \\\n",
       "0  12337.788086   98983.453125  11112.787109   89526.882812  ...    0    0   \n",
       "1  11195.372070   84728.875000  10027.208008   76764.468750  ...    0    0   \n",
       "2   9503.373047   84162.937500   8453.971680   76219.468750  ...    0    0   \n",
       "3  25667.646484  258111.687500  23144.935547  234477.468750  ...    0    0   \n",
       "4  25071.730469  244930.578125  22614.595703  221877.000000  ...    0    0   \n",
       "\n",
       "           P31           P32          P33           P34          P35  \\\n",
       "0   374.969391   9960.210938   103.178192   5157.202148   199.192825   \n",
       "1   125.636726   6339.634766    27.254568   4400.720703    60.425423   \n",
       "2    44.110538   7327.410156     3.616076   1053.424927     9.274529   \n",
       "3  1253.976929  20884.535156  1221.493164  25805.855469  1620.159546   \n",
       "4  1403.122559  24976.859375   904.027893  20220.417969  1234.554077   \n",
       "\n",
       "            P36          P37           P38  \n",
       "0   6606.042969  1900.335693  18949.912109  \n",
       "1   5197.659180   311.825806  13050.573242  \n",
       "2   1630.518555    11.553834   3542.782715  \n",
       "3  30538.640625  5010.981934  55917.894531  \n",
       "4  24038.703125  5224.838867  48107.187500  \n",
       "\n",
       "[5 rows x 34 columns]"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 读取Excel文件，忽略前6行\n",
    "df0 = pd.read_csv('/home/shuai/视频/20240318.csv', skiprows=6)\n",
    "df0 = df0.drop(df0.columns[[0,1,3]+list(range(5, 19, 1))+list(range(-1, -5, -1))], axis=1)\n",
    "display(df0.head())\n",
    "df0_rows = int(df0.shape[0]/9)\n",
    "df0_cols = int(df0.shape[1]-2)\n",
    "df0_values = df0.values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义插值函数\n",
    "def interpolate_surface(x, y, z):\n",
    "    # 创建网格点\n",
    "    xi = np.linspace(x.min(), x.max(), 100)\n",
    "    yi = np.linspace(y.min(), y.max(), 100)\n",
    "    xi, yi = np.meshgrid(xi, yi)\n",
    "    \n",
    "    # 进行插值\n",
    "    zi = griddata((x, y), z, (xi, yi), method='cubic')\n",
    "    return xi, yi, zi\n",
    "\n",
    "# 循环处理每个子图\n",
    "for j in range(df0_cols):\n",
    "    for i in range(df0_rows):\n",
    "        x = df0_values[i*9:(i+1)*9, 1]\n",
    "        y = df0_values[i*9:(i+1)*9, 0]\n",
    "        z = df0_values[i*9:(i+1)*9, j+2] / 10000\n",
    "        \n",
    "        # 插值得到曲面\n",
    "        xi, yi, zi = interpolate_surface(x, y, z)\n",
    "        \n",
    "        # 创建一个三维坐标系\n",
    "        fig = plt.figure(figsize=(5,5))\n",
    "        ax = fig.add_subplot(111, projection='3d')\n",
    "\n",
    "        # 绘制曲面\n",
    "        ax.plot_surface(xi, yi, zi, cmap=cm.coolwarm)\n",
    "\n",
    "        jun_zui = '均值' if j%2==0 else '最值'\n",
    "        # 绘制与zi=50相交的曲面\n",
    "        # ax.contourf(xi, yi, zi, [50], colors='black', linestyles='solid', linewidths=3)\n",
    "        if np.any(zi >=49): \n",
    "            ax.contourf(xi, yi, zi, levels=[49.9,50.1], colors='black')\n",
    "        if np.any(zi >=195): \n",
    "            ax.contourf(xi, yi, zi, levels=[195.9,196.1], colors='green')\n",
    "            print(f'{i*45}度缆绳{int(j/2)+1}张力{jun_zui}')\n",
    "\n",
    "        # 绘制散点图\n",
    "        ax.scatter(x, y, z)\n",
    "\n",
    "        # 设置坐标轴标签\n",
    "        ax.set_xlabel('流速(m/s)')\n",
    "        ax.set_ylabel('波高(m)')\n",
    "        ax.set_zlabel('缆绳张力(t)')\n",
    "        plt.title(f'{i*45}度缆绳{int(j/2)+1}张力{jun_zui}')\n",
    "\n",
    "        plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)\n",
    "\n",
    "        # 保存图形\n",
    "        plt.savefig(f'{folder_path}/{i*45}度缆绳{int(j/2)+1}张力{jun_zui}.jpg')\n",
    "\n",
    "        # 关闭图形，以便下一次循环创建新的图形\n",
    "        plt.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>P2</th>\n",
       "      <th>P4</th>\n",
       "      <th>P39</th>\n",
       "      <th>P40</th>\n",
       "      <th>P41</th>\n",
       "      <th>P42</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1.5</td>\n",
       "      <td>0.5144</td>\n",
       "      <td>-0.025380</td>\n",
       "      <td>0.017596</td>\n",
       "      <td>76.903023</td>\n",
       "      <td>77.019470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.0290</td>\n",
       "      <td>-0.011499</td>\n",
       "      <td>0.019110</td>\n",
       "      <td>76.926567</td>\n",
       "      <td>77.064766</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1.5</td>\n",
       "      <td>1.5430</td>\n",
       "      <td>-0.006904</td>\n",
       "      <td>0.016063</td>\n",
       "      <td>76.961044</td>\n",
       "      <td>77.073494</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2.0</td>\n",
       "      <td>0.5144</td>\n",
       "      <td>-0.034664</td>\n",
       "      <td>0.046643</td>\n",
       "      <td>76.925583</td>\n",
       "      <td>77.244514</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.0</td>\n",
       "      <td>1.0290</td>\n",
       "      <td>-0.033787</td>\n",
       "      <td>0.054192</td>\n",
       "      <td>76.951576</td>\n",
       "      <td>77.203522</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    P2      P4       P39       P40        P41        P42\n",
       "0  1.5  0.5144 -0.025380  0.017596  76.903023  77.019470\n",
       "1  1.5  1.0290 -0.011499  0.019110  76.926567  77.064766\n",
       "2  1.5  1.5430 -0.006904  0.016063  76.961044  77.073494\n",
       "3  2.0  0.5144 -0.034664  0.046643  76.925583  77.244514\n",
       "4  2.0  1.0290 -0.033787  0.054192  76.951576  77.203522"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/plain": [
       "4"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 读取Excel文件，忽略前6行\n",
    "df1 = pd.read_csv('/home/shuai/视频/20240318.csv', skiprows=6)\n",
    "df1 = df1.iloc[:, [2, 4]].join(df1.iloc[:, -4:])\n",
    "display(df1.head())\n",
    "df1_rows = int(df1.shape[0]/9)\n",
    "df1_cols = int(df1.shape[1]-2)\n",
    "df1_values = df1.values\n",
    "display(df1_rows, df1_cols)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义插值函数\n",
    "def interpolate_surface(x, y, z):\n",
    "    # 创建网格点\n",
    "    xi = np.linspace(x.min(), x.max(), 100)\n",
    "    yi = np.linspace(y.min(), y.max(), 100)\n",
    "    xi, yi = np.meshgrid(xi, yi)\n",
    "    \n",
    "    # 进行插值\n",
    "    zi = griddata((x, y), z, (xi, yi), method='cubic')\n",
    "    return xi, yi, zi\n",
    "\n",
    "# 循环处理每个子图\n",
    "for j in range(df1_cols):\n",
    "    for i in range(df1_rows):\n",
    "        x = df1_values[i*9:(i+1)*9, 1].reshape(-1)\n",
    "        y = df1_values[i*9:(i+1)*9, 0].reshape(-1)\n",
    "        z = df1_values[i*9:(i+1)*9, j+2].reshape(-1)\n",
    "        \n",
    "        # 插值得到曲面\n",
    "        xi, yi, zi = interpolate_surface(x, y, z)\n",
    "        \n",
    "        # 创建一个三维坐标系\n",
    "        fig = plt.figure(figsize=(5,5))\n",
    "        ax = fig.add_subplot(111, projection='3d')\n",
    "\n",
    "        # 绘制曲面\n",
    "        ax.plot_surface(xi, yi, zi, cmap=cm.coolwarm)\n",
    "\n",
    "        heng_zong = '横荡' if int(j/2)==0 else '纵荡'\n",
    "        jun_zui = '均值' if j%2==0 else '最值'\n",
    "        # 绘制与zi=50相交的曲面\n",
    "        if np.any(zi >=49): \n",
    "            ax.contourf(xi, yi, zi, levels=[49.9,50.1], colors='black')\n",
    "        if np.any(zi >=195): \n",
    "            ax.contourf(xi, yi, zi, levels=[195.9,196.1], colors='green')\n",
    "            print(f'{i*45}度船舶{heng_zong}{jun_zui}')\n",
    "\n",
    "        # 绘制散点图\n",
    "        ax.scatter(x, y, z)\n",
    "        # 设置坐标轴标签\n",
    "        ax.set_xlabel('流速(m/s)')\n",
    "        ax.set_ylabel('波高(m)')\n",
    "        ax.set_zlabel(f'{heng_zong}(m)')\n",
    "        plt.title(f'{i*45}度船舶{heng_zong}{jun_zui}')\n",
    "\n",
    "        plt.subplots_adjust(left=0.1, right=0.8, top=0.9, bottom=0.1)\n",
    "\n",
    "        # 保存图形\n",
    "        plt.savefig(f'{folder_path}/{i*45}度船舶{heng_zong}{jun_zui}.jpg')\n",
    "\n",
    "        # 关闭图形，以便下一次循环创建新的图形\n",
    "        plt.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "yolo_learn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
